﻿using Ninject;
using System;
using System.Web.Mvc;
using System.Web.Routing;
using HotelManagement.Repository;

namespace HotelManagementApi
{
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private readonly IKernel _kernel;

        public NinjectControllerFactory()
        {
            _kernel = new StandardKernel();
            AddBindings();
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return controllerType == null
                ? null
                : (IController)_kernel.Get(controllerType);
        }

        /// <summary>
        /// 添加绑定
        /// </summary>
        private void AddBindings()
        {
            //绑定接口和实现类
            _kernel.Bind<IRepository>().To<Repository>();
        }
    }
}